//
//  DataPlist.m
//  GaojiaoSNS
//
//  Created by Ruijie on 13-10-28.
//  Copyright (c) 2013年 EbolaPro. All rights reserved.
//

#import "DataPlist.h"

@implementation DataPlist

//+(NSMutableDictionary *)openDPWithName :(NSString *)theName AndTheType :(NSString *)theType
//{
//	//沙盒中Plist文件的路径
//	NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//    NSString *name_type=[NSString stringWithFormat:@"%@.%@",theName,theType];
//	NSString *plist_Path = [docPath stringByAppendingPathComponent:name_type];
//	//原始Plist文件路径
//	NSFileManager *fm = [NSFileManager defaultManager];//文件管理器
//	if(![fm fileExistsAtPath:plist_Path])//如果plist文件不在doc下，copy过来
//	{
//        if([theName hasSuffix:@"Info"])
//        {
//            NSString *originFilePath = [[NSBundle mainBundle] pathForResource:theName ofType:theType];
//            NSError *error = nil;
//            if([fm copyItemAtPath:originFilePath toPath:plist_Path error:&error] == NO)
//            {
//                NSLog(@"创建Plist的时候出现了错误：%@",[error localizedDescription]);
//            }
//        }
//        else
//        {
//            [fm createFileAtPath:plist_Path contents:nil attributes:nil];
//        }
//	}
//    NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithContentsOfFile:plist_Path];
//	NSLog(@"open plist at path:%@",plist_Path);
//	return dic;
//}


/* 读document中的Plist*/
+(NSMutableDictionary *)openPlistFromDocumentWithName :(NSString *)theName AndTheType :(NSString *)theType
{
	//沙盒中Plist文件的路径
	NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *name_type=[NSString stringWithFormat:@"%@.%@",theName,theType];
	NSString *plist_Path = [docPath stringByAppendingPathComponent:name_type];
	//原始Plist文件路径
	NSFileManager *fm = [NSFileManager defaultManager];//文件管理器
	if(![fm fileExistsAtPath:plist_Path])//如果plist文件不在doc下,返回空内容
	{
        NSLog(@"获取失败 plist at path:%@",plist_Path);
        return nil;
	}
    NSMutableDictionary *dic=[[NSMutableDictionary alloc]initWithContentsOfFile:plist_Path];
	NSLog(@"获取成功 plist at path:%@",plist_Path);
	return dic;
}
/* 写入document中的Plist*/
+(void)writePlistToDocumentWithDic:(NSMutableDictionary *)theDic WithName :(NSString *)theName AndTheType :(NSString *)theType
{
    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *name_type=[NSString stringWithFormat:@"%@.%@",theName,theType];
	NSString *plist_Path = [docPath stringByAppendingPathComponent:name_type];
	NSLog(@"write %@ plist at path:%@",theDic,plist_Path);
    
    if( [theDic writeToFile:plist_Path atomically:YES])
    {
        NSLog(@"Plist 写入成功 ");
    }
    else
    {
        NSLog(@"Plist 写入失败 ");
    }
}

//内容写入到tmp文件夹下的子文件夹,返回data路径
+ (NSString *)writeData :(NSData *)theData ToTMPWithSubFloderName :(NSString *)theFloderName FileName :(NSString *)theFileName
{
    if (!theData) {
        NSLog(@"%s ,writting Data is nil",__FUNCTION__);
        return @"";
    }
    
    NSString *temppath=NSTemporaryDirectory();
    NSString *floderPath=[temppath stringByAppendingPathComponent:theFloderName];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL isDir = NO;
    BOOL existed = [fileManager fileExistsAtPath:floderPath isDirectory:&isDir];
    if ( !(isDir == YES && existed == YES) )
    {
        [fileManager createDirectoryAtPath:floderPath withIntermediateDirectories:YES attributes:nil error:nil];
    }
    NSString *filePath=[floderPath stringByAppendingPathComponent:theFileName];
    if([theData writeToFile:filePath atomically:YES])
    {
        NSLog(@"Data 写入成功 filePath=%@",filePath);
        return filePath;
    }
    else
    {
        NSLog(@"Data 写入失败 filePath=%@",filePath);
        return @"";
    }
    
}
@end
